/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vertexnet.realestate.view.dialog;

import static com.vertexnet.realestate.constants.UIConstants.LAYOUT;

import java.math.BigDecimal;
import java.text.NumberFormat;

import javax.swing.JOptionPane;

import com.vertexnet.realestate.interfaces.Controller;
import com.vertexnet.realestate.interfaces.DefaultDialog;
import com.vertexnet.realestate.interfaces.MainView;
import com.vertexnet.realestate.interfaces.View;
import com.vertexnet.realestate.model.RealEstateUtilityModel;
import com.vertexnet.realestatenet.model.bean.Layout;

/**
 * 
 * @author Pawan
 */
public class LayoutDetailsDialog extends DefaultDialog implements View {

	private static final long serialVersionUID = 1L;
	private Layout layout;
	private Controller controller;

	/**
	 * Creates new form LayoutDetailsDialog
	 * 
	 * @param controller
	 */
	public LayoutDetailsDialog(Layout layout, Controller controller) {
		initComponents();

		this.controller = controller;
		this.layout = layout;
		setupDialog();
	}

	public LayoutDetailsDialog(Controller controller) {
		this(new Layout(), controller);
	}

	@Override
	public void setupDialog() {
		layoutNameTextField.setText(layout.getName());
		if (layout.getTotalSquareFeet() != null) {
			totalSquareFeetTextField.setText(layout.getTotalSquareFeet().toString());
		}
		if (layout.getNumSites() != 0) {
			noSitesTextField.setText(new Integer(layout.getNumSites()).toString());
		}
		addressTextArea.setText(layout.getAddress());
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        layoutDialogName = new javax.swing.JLabel();
        layoutNameTextField = new javax.swing.JTextField();
        totalSquareFeetLabel = new javax.swing.JLabel();
        totalSquareFeetTextField = new javax.swing.JFormattedTextField(NumberFormat.getInstance());
        noSitesLabel = new javax.swing.JLabel();
        noSitesTextField = new javax.swing.JFormattedTextField(NumberFormat.getInstance());
        addressLabel = new javax.swing.JLabel();
        saveButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();
        addressScrollPane = new javax.swing.JScrollPane();
        addressTextArea = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        layoutDialogName.setText("Layout Name : ");

        totalSquareFeetLabel.setText("Total Square Feet : ");

        noSitesLabel.setText("Number of Sites : ");

        addressLabel.setText("Address : ");

        saveButton.setText("Save");
        saveButton.setName(LAYOUT.getSave());
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        closeButton.setText("Close");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        addressTextArea.setColumns(20);
        addressTextArea.setLineWrap(true);
        addressTextArea.setRows(2);
        addressScrollPane.setViewportView(addressTextArea);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(totalSquareFeetLabel)
                            .addComponent(noSitesLabel)
                            .addComponent(addressLabel)
                            .addComponent(layoutDialogName))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(layoutNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(noSitesTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(totalSquareFeetTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(addressScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
                                .addContainerGap())))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(saveButton)
                        .addGap(7, 7, 7)
                        .addComponent(closeButton)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(layoutDialogName)
                    .addComponent(layoutNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(totalSquareFeetLabel)
                    .addComponent(totalSquareFeetTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(noSitesLabel)
                    .addComponent(noSitesTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(addressLabel)
                    .addComponent(addressScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveButton)
                    .addComponent(closeButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_saveButtonActionPerformed
		StringBuffer string = new StringBuffer();
		if (layoutNameTextField.getText().isEmpty()) {
			string.append("Layout Name cannot be Empty\n");
		} else {
			// Layout Id Repeatancy test
			for (Layout layout : RealEstateUtilityModel.layouts) {
				if (this.layout != layout) {
					if (layout.getName().equals(layoutNameTextField.getText())) {
						JOptionPane.showMessageDialog(this, "A Layout with Same Name already exists");
						return;
					}
				}
			}
		}

		if (totalSquareFeetTextField.getText().trim().isEmpty()) {
			string.append("Total Square Feet cannot be Empty/Text \n");
		}
		if (noSitesTextField.getText().isEmpty()) {
			string.append("Number of Sites cannot be Empty \n");
		}
		if (addressTextArea.getText().trim().isEmpty()) {
			string.append("Address cannot be Empty\n");
		}
		if (!string.toString().isEmpty()) {
			JOptionPane.showMessageDialog(this, string.toString());
			return;
		}

		layout.setName(layoutNameTextField.getText());
		layout.setTotalSquareFeet(new BigDecimal(totalSquareFeetTextField.getText().replace(",", "")));
		layout.setNumSites(Integer.parseInt(noSitesTextField.getText().replace(",", "")));
		layout.setAddress(addressTextArea.getText());

		controller.handleEvent(evt);
	}// GEN-LAST:event_saveButtonActionPerformed

	private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_closeButtonActionPerformed
		this.dispose();
	}// GEN-LAST:event_closeButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel addressLabel;
    private javax.swing.JScrollPane addressScrollPane;
    private javax.swing.JTextArea addressTextArea;
    private javax.swing.JButton closeButton;
    private javax.swing.JLabel layoutDialogName;
    private javax.swing.JTextField layoutNameTextField;
    private javax.swing.JLabel noSitesLabel;
    private javax.swing.JFormattedTextField noSitesTextField;
    private javax.swing.JButton saveButton;
    private javax.swing.JLabel totalSquareFeetLabel;
    private javax.swing.JFormattedTextField totalSquareFeetTextField;
    // End of variables declaration//GEN-END:variables

	public void setRealEstateFrame(MainView mainView) {
	}

	public void setController(Controller controller) {
		this.controller = controller;
	}

	public Layout getLayoutObject() {
		return layout;
	}

	public void showSaveConfirmation() {
		JOptionPane.showMessageDialog(this, "Data Saved Successfully");
	}
}
